//
//  MSVersion.m
//  Musou
//
//  Created by luo danal on 7/20/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "MSVersion.h"

@implementation MSVersion
@synthesize appid = _appid;
@synthesize request = _request;

- (void)dealloc{
    _request.delegate = nil;
    [_request release];
    [_appid release];
    [super dealloc];
}

- (id)initWithAPPID:(NSString *)appid completion:(void (^)(NSString *))block{
    if ((self = [super init])) {
        self.appid = appid;
        _completionBlock = Block_copy(block);
    }
    return self;
}

- (void)detect{
    self.request = [MSHTTPRequest requestWithDelegate:self];
    self.request.parseJson = YES;
    self.request.URL = [NSURL URLWithString:[NSString stringWithFormat:kLoopupApi,self.appid]];
    [self.request start];
    do {
        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
    } while (!_done);
}

+ (void)detectWithAPPID:(NSString *)appid completion:(void (^)(NSString *))block{
    MSVersion *version = [[[self alloc] initWithAPPID:appid completion:block] autorelease];
    [version detect];
}

#pragma mark - Delegate
- (void)requestDidFinishLoading:(MSHTTPRequest *)req error:(NSError *)error{
    _done = YES;
    NSDictionary *json = req.result;
//    NSLog(@"%@",json);
    int resultCount = [[json objectForKey:@"resultCount"] intValue];
    if (resultCount > 0) {
        NSArray *results = [json objectForKey:@"results"];
        NSDictionary *d = [results objectAtIndex:0];
        NSString *version = [d objectForKey:@"version"];
        NSString *name = [d objectForKey:@"trackName"];
#ifdef DEBUG
        NSLog(@"LAST VERSION:[%@] FOR APP [%@]",version,name);
#endif
        if (_completionBlock != NULL) {
            _completionBlock(version);
        }
    }
}

@end
